package com.cainaer.api.controller;

import com.cainaer.common.core.sms.SmsCode;
import com.cainaer.common.core.utils.MobileUtil;
import com.cainaer.common.core.utils.StringUtils;
import com.cainaer.common.core.web.domain.AjaxResult;
import com.cainaer.common.redis.service.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author-> tina
 * @date-> 2020/9/5 下午5:05
 * @version-> 1.0
 * @description->
 **/
@RestController
@RequestMapping("/sms/")
public class SmsController {

    private Logger logger = LoggerFactory.getLogger(SmsController.class);

    @Autowired
    private RedisService redisSerice;

    @PostMapping("code/{tel}")
    public AjaxResult code(@PathVariable("tel")  String tel) {
        if (StringUtils.isEmpty(tel)){
            return AjaxResult.error("号码不能为空");
        }

        boolean mobileNum = MobileUtil.isMobileNum(tel);
        if (!mobileNum) {
            return AjaxResult.error("号码不正确");
        }
        String send = SmsCode.send(tel);

        logger.info("sendCode->{}", send);
        boolean set = redisSerice.set("smsCode:" + tel + ":"+send, send, 360);
        logger.info("setSendCode->{}", set);
        return AjaxResult.success("发送成功", send);
    }

}
